<?php

namespace common\modules\article\models\backend;

use common\behaviors\UploadBehavior;
use common\widgets\ActiveForm;
use common\widgets\CKEditor;
use common\widgets\FileApiWidget;
use common\widgets\FlowChart;
use common\widgets\Form;
use common\widgets\Select2;
use Yii;

/**
 * This is the form model class for model "Item".
 */
class ItemForm extends Item
{

    /**
     * @inheritdoc
     */
    public $isNewRecord;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return parent::rules();
    }

    const SCENARIO_CREATE = 'admin-create';
    const SCENARIO_UPDATE = 'admin-update';

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'admin-create' => [
                'id',
                'category_id',
                'title',
                'image',
                'description',
                'text',
                'slug',
                'views',
                'status',
                'created_at',
                'updated_at',
                'created_by',
                'updated_by',
            ],
            'admin-update' => [
                'id',
                'category_id',
                'title',
                'image',
                'description',
                'text',
                'slug',
                'views',
                'status',
                'created_at',
                'updated_at',
                'created_by',
                'updated_by',
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'uploadBehavior' => [
                'class'      => UploadBehavior::className(),
                'attributes' => [
                    'image' => [
                        'path'     => Yii::$app->params['uploadCommonPath'],
                        'tempPath' => Yii::$app->params['uploadCommonTempPath'],
                        'url'      => Yii::$app->params['uploadCommonUrl'],
                    ],
                ],
            ],
        ];
    }

    /**
     * 返回当前表单的名称
     *
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/article', 'Item');
    }

    /**
     * 列表定义
     *
     * @param ActiveForm|Form $form
     * @param                 $model ItemForm
     * @param mixed           $extra
     * @return array
     * @throws \yii\web\ServerErrorHttpException
     */
    public function formRows(&$form, $model, $extra)
    {
        return [
            [
                'columns'    => 2,
                'attributes' => [
                    'id' => [
                        'type'    => Form::INPUT_TEXT,
                        'options' => ! $model->isNewRecord ? ['disabled' => true] : [],
                    ],

                    'category_id' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'data' => CategoryTree::getTreeList(),
                        ],
                    ],
                ],
            ],
            [
                'columns'    => 1,
                'attributes' => [
                    'title' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                ],
            ],
            [
                'columns'    => 2,
                'attributes' => [
                    'image' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => FileApiWidget::className(),
                        'options'     => [
                            'settings' => [
                                'url' => [Yii::$app->params['uploadCommonHandler']],
                            ],
                        ],
                    ],

                    'description' => [
                        'type'    => Form::INPUT_TEXTAREA,
                        'options' => [
                            'rows' => 6,
                        ],
                    ],
                ],
            ],
            [
                'columns'    => 1,
                'attributes' => [

                    'text' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => CKEditor::className(),
                    ],

                    'status' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => FlowChart::className(),
                    ],
                ],
            ],
            [
                'columns'    => 2,
                'attributes' => [
                    'slug' => [
                        'type' => Form::INPUT_TEXT,
                    ],

                    'views' => [
                        'type' => Form::INPUT_TEXT,
                    ],
                ],
            ],
            [
                'columns'    => 2,
                'attributes' => [
                    'created_at' => [
                        'type'        => Form::INPUT_STATIC,
                        'staticValue' => function ($model, $key, $index)
                        {
                            /**
                             * @param $this $model
                             */
                            return $model->created_at
                                ? date('Y-m-d H:i:s', $model->created_at)
                                : '--';
                        },
                    ],

                    'updated_at' => [
                        'type'        => Form::INPUT_STATIC,
                        'staticValue' => function ($model, $key, $index)
                        {
                            /**
                             * @param $this $model
                             */
                            return $model->updated_at
                                ? date('Y-m-d H:i:s', $model->updated_at)
                                : '--';
                        },
                    ],
                ],
            ],
        ];
    }

}
